﻿/********************************************************************************
* Author:	Christos Polydorou	(xristos.polydoroy@gmail.com)					*
* Author:	Aris Michail		(aris.michail@gmail.com)						*
* Date:		22/9/2011															*
********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{

    public partial class UserManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Session["isValid"].ToString().Equals("true"))
                    Response.Redirect("login.aspx");
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                Response.Redirect("login.aspx");
            }

            DataClassesDataContext dc = new DataClassesDataContext();
            User usr =
                (from u in dc.Users
                 where u.Username.Equals(Session["username"])
                 select u).Single();
            if (!usr.Usertype.Equals("adm"))
                Response.Redirect("default.aspx");

            HideMenu();


            this.Label5.Text = "";
            this.TextBox1.Visible = false;
            this.TextBox2.Visible = false;
            this.Button5.Visible = false;


            this.DropDownList1.DataSource = from p in (new DataClassesDataContext()).Users
                                            orderby p.Username
                                            select new { ID = p.ID, Username = p.Username };
            this.DropDownList1.DataTextField = "Username";
            this.DropDownList1.DataValueField = "ID";
            if (!IsPostBack)
                this.DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button1_Click(sender, e);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            User usr =
                (from u in dc.Users
                 where u.ID.Equals(this.DropDownList1.SelectedItem.Value)
                 select u).Single();


            this.Label1.Text = "Όνομα Χρήστη: " + usr.Username;
            this.Label2.Text = "Διεύθυνση Email: " + usr.Email;
            this.Label3.Text = "Όνομα: " + usr.fname;
            this.Label4.Text = "Επώνυμο: " + usr.lname;

            if (usr.IsEnabled != 0)
                this.Button2.Text = "Απενεργοποίηση";
            else
                this.Button2.Text = "Ενεργοποίηση";

            if (usr.Usertype.Equals("std"))
                this.Button3.Text = "Προαγωγή σε Διαχειριστή";
            else
                this.Button3.Text = "Επιστροφή σε Χρήστη";
            RevealMenu();
        }


        private void HideMenu()
        {
            this.Label1.Visible = false;
            this.Label2.Visible = false;
            this.Label3.Visible = false;
            this.Label4.Visible = false;
            this.Button2.Visible = false;
            this.Button3.Visible = false;
            this.Button4.Visible = false;
        }

        private void RevealMenu()
        {
            this.Label1.Visible = true;
            this.Label2.Visible = true;
            this.Label3.Visible = true;
            this.Label4.Visible = true;
            this.Button2.Visible = true;
            this.Button3.Visible = true;
            this.Button4.Visible = true;
        }
        protected void DropDownList1_TextChanged(object sender, EventArgs e)
        {
            this.Button1_Click(sender, e);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var user =
                (from r in dc.Users
                 where r.ID.Equals(this.DropDownList1.SelectedValue)
                 select r).Single();
            if (this.Button2.Text.Equals("Ενεργοποίηση"))
            {
                user.IsEnabled = 1;
                HttpContext.Current.Session["message"] = "Ο χρήστης " + user.Username + " ενεργοποιήθηκε.";
            }
            else
            {
                user.IsEnabled = 0;
                HttpContext.Current.Session["message"] = "Ο χρήστης " + user.Username + " απενεργοποιήθηκε.";
            }

            dc.SubmitChanges();
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var user =
                (from r in dc.Users
                 where r.ID.Equals(this.DropDownList1.SelectedValue)
                 select r).Single();
            if (this.Button3.Text.Equals("Προαγωγή σε Διαχειριστή"))
            {
                user.Usertype = "adm";
                HttpContext.Current.Session["message"] = "Ο χρήστης " + user.Username + " είναι τώρα διαχειριστής.";
            }
            else
            {
                user.Usertype = "std";
                HttpContext.Current.Session["message"] = "Ο χρήστης " + user.Username + " είναι τώρα απλός χρήστης";
            }
            dc.SubmitChanges();
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            this.TextBox1.TextMode = TextBoxMode.Password;
            this.TextBox2.TextMode = TextBoxMode.Password;
            this.Button5.Visible = true;
            this.Label3.Text = "Νέο συνθηματικό ";
            this.Label4.Text = "Επιβεβαίωση νέου συνθηματικού ";
            this.Label3.Visible = true;
            this.Label4.Visible = true;
            this.TextBox1.Visible = true;
            this.TextBox2.Visible = true;
        }
        protected void Button5_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var user =
                (from r in dc.Users
                 where r.ID.Equals(this.DropDownList1.SelectedValue)
                 select r).Single();
            if (this.TextBox1.Text.Equals(this.TextBox2.Text))
            {
                user.Password = this.TextBox1.Text;
                dc.SubmitChanges();
                HttpContext.Current.Session["message"] = "Η αλλαγή του συνθηματικού ήταν επιτυχής";
            }
            else
                HttpContext.Current.Session["message"] = "Τα συνθηματικά πρέπει να είναι ίδια";
            return;
        }
    }
}